//
//  KDMListCell.m
//  PublicSentimentMonitorSystem
//
//  Created by 魏强 on 15/11/2.
//  Copyright © 2015年 魏强. All rights reserved.
//

#import "KDMListCell.h"
#import "KDMListCellBaseModel.h"
#import "KDHomeRequestTool.h"
#import "KDHomeListPageCollectParam.h"
#import "KDHomeListPageCollectResult.h"
#import "KDHomeListPageUnCollectResult.h"
#import "KDHomeListPageUnCollectParam.h"
#import "SVProgressHUD.h"
@interface KDMListCell ()
/**
 *  cell底部的view
 */
@property (weak, nonatomic) IBOutlet UIView *underView;
/**
 *  标题label
 */
@property (weak, nonatomic) IBOutlet UILabel *titleLb;
@property (weak, nonatomic) IBOutlet UILabel *comeFromLb;
@property (weak, nonatomic) IBOutlet UILabel *timeLb;
@property (weak, nonatomic) IBOutlet UIButton *collectBtn;
/**
 *  相关
 */
@property (weak, nonatomic) IBOutlet UIButton *relatedBtn;
/**
 *  数字
 */
@property (weak, nonatomic) IBOutlet UILabel *numLb;
/**
 *  上报
 */
@property (weak, nonatomic) IBOutlet UIButton *reportBtn;
/**
 *  数字lb的宽度约束
 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *numLb_constraint_W;
/**
 *  相关文字距离中心的偏离
 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *relationLb_CenterX_Constraint;

@end

@implementation KDMListCell

- (void)awakeFromNib {
    // Initialization code
}
- (void)setHighlight:(BOOL)highlight{
    _highlight = highlight;
    if (highlight) {
        self.titleLb.textColor = WQColor(178, 182, 184);
        self.model.read = YES;
    }
    //    [self layoutIfNeeded];
}

+ (instancetype)cellWithTableView:(UITableView *)tableView andVersion:(NSInteger)version{
    static NSString *cellId =@"KDMListCell";
    
//    UINib *xib =  (version)? [UINib nibWithNibName:@"KDMListCell2" bundle:nil] :[UINib nibWithNibName:@"KDMListCell" bundle:nil];
//    [tableView registerNib:xib forCellReuseIdentifier:cellId];
    KDMListCell *cell =[tableView dequeueReusableCellWithIdentifier:cellId];
    cell.backgroundColor = BackGroundColor;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    cell.underView.layer.borderWidth = 0.2f;
//    cell.underView.layer.borderColor = [UIColor lightGrayColor].CGColor;
//    cell.numLb.layer.cornerRadius = 4.0;
    cell.numLb.clipsToBounds = YES;
    [cell.reportBtn setImage:[UIImage imageNamed:@"已上报"] forState:UIControlStateDisabled];
    //监听收藏状态改变之后的通知
    [[NSNotificationCenter defaultCenter] addObserver:cell selector:@selector(SingleWebViewControllerCollectionBtnClick:) name:@"SingleWebViewControllerCollectionBtnClickToMonitorNoty" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:cell selector:@selector(SingleWebViewControllerCollectionBtnClick:) name:@"SingleWebViewControllerCollectionBtnClickToSearchNoty" object:nil];
    //监听相关界面的收藏状态改变了的通知
    [[NSNotificationCenter defaultCenter] addObserver:cell selector:@selector(SingleWebViewControllerCollectionBtnClick:) name:@"RelationControllerCollectionBtnClickToMonitorNoty" object:nil];
    //监听上报成功之后的通知
    [[NSNotificationCenter defaultCenter] addObserver:cell selector:@selector(ReportControllerReportSeccess:) name:@"ReportControllerReportSeccessToMonitorNoty" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:cell selector:@selector(ReportControllerReportSeccess:) name:@"ReportControllerReportSeccessToSearchNoty" object:nil];

    return cell;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    
}
- (void)setModel:(KDMListCellBaseModel *)model{
    _model = model;
    NSString * str = [self.model.title stringByReplacingOccurrencesOfString:@"<em>" withString:@""];
    str = [str stringByReplacingOccurrencesOfString:@"</em>" withString:@""];
    self.titleLb.text = str;
    self.comeFromLb.text = model.source;
    self.timeLb.text = model.date;
    if (model.read) {
        self.titleLb.textColor = WQColor(178, 182, 184);
    }else{
        self.titleLb.textColor = [UIColor blackColor];
    }
//    设置相关条数的显示
    if (model.topicCount == 1) {//只有一条的时候就不显示
        self.numLb.text = [NSString stringWithFormat:@"%ld", (long)model.topicCount - 1];
        self.numLb.hidden = YES;
        self.relatedBtn.enabled = NO;
        self.numLb_constraint_W.constant = 0;
        self.relationLb_CenterX_Constraint.constant = 0;
    }else{
        self.numLb.text = [NSString stringWithFormat:@"%ld", (long)model.topicCount];
        self.relatedBtn.enabled = YES;
        self.numLb.hidden = NO
        ;
        self.relationLb_CenterX_Constraint.constant = 17;
        if (model.topicCount < 10) {//条数小于10 的时候变圆
            self.numLb_constraint_W.constant = 13;
            self.numLb.layer.cornerRadius = 6;
            self.numLb.clipsToBounds = YES;
            if (model.topicCount-1 == 0) {
                self.numLb.backgroundColor = [UIColor grayColor];
            }else{
                self.numLb.backgroundColor = WQColor(255, 106, 18);
            }
        }else{
            self.numLb_constraint_W.constant = 30;
            self.numLb.backgroundColor = WQColor(255, 106, 18);
        }
    }
    
    self.collectBtn.selected = model.isSave;
    self.reportBtn.selected = model.report;

}
#pragma  mark - 事件点击区
- (IBAction)reportBtnClick:(UIButton *)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(KDMListCell:reportBtnClick:)]) {
        [_delegate KDMListCell:self reportBtnClick:sender];
    }
    
}
- (IBAction)collectBtnClick:(UIButton *)sender {
    if (!self.collectBtn.selected) {
        [self sendCollectData];
    }else{
        [self sendUncollectData];
    }
}
/**
 *  相关按钮被点击了
 */
- (IBAction)relatedBtnClick:(UIButton *)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(KDMListCell:relationBtnClick:)]) {
        [_delegate KDMListCell:self relationBtnClick:sender];
    }
}

- (void)sendCollectData
{
    KDAccount *account = [KDAccountTool account];
    KDHomeListPageCollectParam *param = [[KDHomeListPageCollectParam alloc]init];
    param.method = @"addFavorite";
    param.docId = self.model.docId;
    param.url = self.model.url;
    param.token = account.token;
    [KDHomeRequestTool sendCollectDataWithParam:param success:^(KDHomeListPageCollectResult *responseObejct) {
        if ([responseObejct.status isEqualToString:@"ok"]) {
            [SVProgressHUD showSuccessWithStatus:@"收藏成功！" maskType:SVProgressHUDMaskTypeGradient];
            self.model.save = !self.model.save;
            self.collectBtn.selected = self.model.isSave;
            if (_delegate && [_delegate respondsToSelector:@selector(KDMListCell:collectBtnClick:)]) {
                [_delegate KDMListCell:self collectBtnClick:self.collectBtn];
            }
        }
    } faiure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"收藏失败！" maskType:SVProgressHUDMaskTypeGradient];
        KDLog(@"%@",error);
    }];
}
- (void)sendUncollectData{
    KDAccount *account = [KDAccountTool account];
    KDHomeListPageUnCollectParam *param = [[KDHomeListPageUnCollectParam alloc]init];
    param.method = @"deleteFav";
    param.token = account.token;
    param.docId = self.model.docId;
    [KDHomeRequestTool sendUnCollectDataWithParam:param success:^(KDHomeListPageUnCollectResult *responseObejct) {
        if ([responseObejct.status isEqualToString:@"ok"]) {
            [SVProgressHUD showSuccessWithStatus:@"取消收藏成功！" maskType:SVProgressHUDMaskTypeGradient];
            self.model.save = !self.model.save;
            self.collectBtn.selected = self.model.isSave;
            if (_delegate && [_delegate respondsToSelector:@selector(KDMListCell:collectBtnClick:)]) {
                [_delegate KDMListCell:self collectBtnClick:self.collectBtn];
            }
        }
    } faiure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"取消收藏失败！" maskType:SVProgressHUDMaskTypeGradient];
        KDLog(@"%@",error);
    }];
    
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - 通知接收区
- (void)SingleWebViewControllerCollectionBtnClick:(NSNotification *)noty{
    NSString *docid = noty.userInfo[@"docid"];
    if ([docid isEqualToString: self.model.docId]) {
        self.model.save = [noty.userInfo[@"isCollect"] boolValue];
        self.collectBtn.selected = self.model.isSave;
    }
}

- (void)ReportControllerReportSeccess:(NSNotification *)noty{
    NSString *docid = noty.userInfo[@"docid"];
    if ([docid isEqualToString: self.model.docId]) {
        self.model.report = YES;
        self.reportBtn.selected = self.model.report;
    }
}
@end
